/**
 * 
 */
package d.qapaper.main;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.graphics.Color;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import d.qapaper.bean.Project;
import d.qapaper.bean.Question;
import d.qapaper.bean.Unit;
import d.qapaper.common.Constants;
import d.qapaper.tool.ImageUtil;

/**
 * 生成控件
 * 
 * @author Kaven 2011-4-26
 */
public class createViewItem {
	Context qa;
	private int itemSize = 26;
	private boolean isShow = true;

	public createViewItem(Context qa, String isShowInput) {
		this.qa = qa;
		if (null != isShowInput && !isShowInput.equalsIgnoreCase("1")) {
			this.isShow = false;
		}
		Constants.questionViewsList=new ArrayList<Map<Integer, Integer>>();
	}

	/**
	 * 生成問卷問題
	 * 
	 * @param qa
	 * @param qus
	 * @return
	 */
	public View createViewItem(Question qus, HashMap<String, String> config) {
		// 控件标识
		int index = 1;
		final int qusId = qus.getId();
		final String qusSplit = "\\|TAG\\|";
		if (null != config.get("item")) {
			itemSize = Integer.parseInt(config.get("item"));
		}
		LinearLayout linearLayout = new LinearLayout(qa);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		TextView titleView = new TextView(qa);
		// 添加標題
		titleView.setText(qus.getId() + "、" + qus.getTitle());
		titleView.setTextColor(Color.parseColor("#545124"));
		titleView.setTextSize(itemSize);
		linearLayout.addView(titleView, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		LinearLayout templLayout = null;
		switch (qus.getType()) {
		case 1:// 普通單選
			if (null != qus.getQans()) {
				String[] qusSets = qus.getQans().split(qusSplit);
				LinearLayout linearLayout2 = new LinearLayout(qa);
				linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
				LinearLayout container = new LinearLayout(qa);
				container.setOrientation(LinearLayout.VERTICAL);
				container.setBackgroundColor(Color.parseColor("#f3f3f3"));
				container.setId(qus.getId());
				Constants.addQuestionViews2List(qus.getId(), 1);
				RadioButton radioButton = null;
				int len = qusSets.length;
				for (String el : qusSets) {
					radioButton = new RadioButton(qa);
					radioButton.setTag(index);
					radioButton.setTextSize(itemSize);

					radioButton.setText(el);
					radioButton.setTextColor(Color.parseColor("#545124"));
					// 设监听器重置该区域radio
					radioButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							RadioButton buttonView = (RadioButton) v;
							LinearLayout radioArea = (LinearLayout) buttonView
									.getParent().getParent();
							LinearLayout templinLayout;
							RadioButton radio = null;
							for (int z = 0; z < radioArea.getChildCount(); z++) {
								templinLayout = (LinearLayout) radioArea
										.getChildAt(z);
								for (int y = 0; y < templinLayout
										.getChildCount(); y++) {
									radio = (RadioButton) templinLayout
											.getChildAt(y);
									if (radio != buttonView) {
										radio.setChecked(false);
									}
									Log.i("info", radio.getTag().toString());
								}
							}
							Log.i("current", buttonView.getTag().toString()
									+ "  -----------");
						}
					});
					Log.i("radio", radioButton.getText().toString());
					if (len > 6) {
						if (index % 6 == 0 && index != 1) {
							templLayout = new LinearLayout(qa);
						}
						if (index <= 6) {
							linearLayout2.addView(radioButton);
						} else {
							templLayout.addView(radioButton);
						}
						if (index == 6) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
						if (index % 7 == 0) {
							container.addView(templLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					} else {
						linearLayout2.addView(radioButton);
						if (index == len - 1) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					}

					index++;
				}
				linearLayout.addView(container, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			break;

		case 2:// 多選
			if (null != qus.getQans()) {
				String[] qusSets = qus.getQans().split(qusSplit);
				LinearLayout checkLayout = new LinearLayout(qa);
				checkLayout.setOrientation(LinearLayout.HORIZONTAL);
				LinearLayout container = new LinearLayout(qa);
				container.setOrientation(LinearLayout.VERTICAL);
				container.setId(qus.getId());
				container.setBackgroundColor(Color.parseColor("#f3f3f3"));
				Constants.addQuestionViews2List(qus.getId(), 2);
				CheckBox checkBox = null;
				int len = qusSets.length;
				for (String el : qusSets) {
					checkBox = new CheckBox(qa);
					checkBox.setTextColor(Color.parseColor("#545124"));
					checkBox.setTag(index);
					checkBox.setTextSize(itemSize);

					checkBox.setText(el);
					Log.i("checkbox", checkBox.getText().toString());
					if (len > 6) {
						if (index % 4 == 1 && index != 1) {
							templLayout = new LinearLayout(qa);

						}
						if (index == 6) {
							container.addView(checkLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
						;
						if (index <= 6) {
							checkLayout.addView(checkBox);
						} else {
							templLayout.addView(checkBox);
						}

						if (index % 7 == 0) {
							container.addView(templLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					} else {
						checkLayout.addView(checkBox);
						if (index == len - 1) {
							container.addView(checkLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					}
					index++;
					// checkLayout.addView(checkBox);
				}
				linearLayout.addView(container, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			break;
		case 3:// 文本框
			LinearLayout textLayout = new LinearLayout(qa);
			EditText editText = new EditText(qa);
			// editText.setBackgroundColor(Color.parseColor("#f3f3f3"));
			editText.setId(qus.getId());
			Constants.addQuestionViews2List(qus.getId(), 3);
			textLayout.addView(editText, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			linearLayout.addView(textLayout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			break;
		case 4:// 文本域
			LinearLayout textareaLayout = new LinearLayout(qa);
			EditText editareaText = new EditText(qa);
			// editareaText.setBackgroundColor(Color.parseColor("#f3f3f3"));
			editareaText.setId(qus.getId());
			Constants.addQuestionViews2List(qus.getId(), 4);
//			editareaText.setHeight(120);
			editareaText.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE
					| InputType.TYPE_CLASS_TEXT);
			editareaText.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			textareaLayout.addView(editareaText);
			linearLayout.addView(textareaLayout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			break;
		case 5:// 滿意度單選，帶文本輸入（非常不滿意時）
			if (null != qus.getQans()) {
				String[] qusSets = qus.getQans().split(qusSplit);
				LinearLayout linearLayout2 = new LinearLayout(qa);
				linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
				LinearLayout container = new LinearLayout(qa);
				container.setOrientation(LinearLayout.VERTICAL);
				container.setBackgroundColor(Color.parseColor("#f3f3f3"));
				container.setId(qus.getId());
				Constants.addQuestionViews2List(qus.getId(), 5);
				RadioButton radioButton = null;
				final EditText reasonEditText = new EditText(qa);
				reasonEditText.setVisibility(View.GONE);
				reasonEditText.setTag(qusId + "_reason");
				int len = qusSets.length;
				for (String el : qusSets) {
					radioButton = new RadioButton(qa);
					radioButton.setTag(index);
					radioButton.setTextSize(itemSize);

					radioButton.setText(el);
					radioButton.setTextColor(Color.parseColor("#545124"));
					radioButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							RadioButton buttonView = (RadioButton) v;
							LinearLayout radioArea = (LinearLayout) buttonView
									.getParent().getParent();
							LinearLayout templinLayout;
							RadioButton radio = null;
							View view = null;
							for (int z = 0; z < radioArea.getChildCount(); z++) {
								view = radioArea.getChildAt(z);
								if (view instanceof LinearLayout) {
									templinLayout = (LinearLayout) radioArea
											.getChildAt(z);
									for (int y = 0; y < templinLayout
											.getChildCount(); y++) {
										radio = (RadioButton) templinLayout
												.getChildAt(y);
										if (radio != buttonView) {
											radio.setChecked(false);
										}
										Log.i("info", radio.getTag().toString());
									}
								}
							}
							if (isShow) {
								if ("5".equalsIgnoreCase(buttonView.getTag()
										.toString())) {
									if (buttonView.isChecked()) {
										if (reasonEditText != null) {
											reasonEditText
													.setVisibility(View.VISIBLE);
										}
									} else {
										if (reasonEditText != null) {
											reasonEditText
													.setVisibility(View.GONE);
										}
									}
								} else {
									if (reasonEditText != null) {
										reasonEditText.setVisibility(View.GONE);
									}
								}
								Log.i("current", buttonView.getTag().toString()
										+ "  -----------");
							}
						}
					});
					if (len > 6) {
						if (index % 6 == 0 && index != 1) {
							templLayout = new LinearLayout(qa);
						}
						if (index <= 6) {
							linearLayout2.addView(radioButton);
						} else {
							templLayout.addView(radioButton);
						}
						if (index == 6) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
						if (index % 7 == 0) {
							container.addView(templLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					} else {
						linearLayout2.addView(radioButton);
						if (index == len - 1) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					}
					index++;

				}
				if (isShow) {
					container.addView(reasonEditText,
							new LayoutParams(LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
				}
				linearLayout.addView(container, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			break;
		case 6:// 國際單選，帶文本框（其他時）
			if (null != qus.getQans()) {
				String[] qusSets = qus.getQans().split(qusSplit);
				LinearLayout linearLayout2 = new LinearLayout(qa);
				linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
				LinearLayout container = new LinearLayout(qa);
				container.setOrientation(LinearLayout.VERTICAL);
				container.setBackgroundColor(Color.parseColor("#f3f3f3"));
				container.setId(qus.getId());
				Constants.addQuestionViews2List(qus.getId(), 6);
				RadioButton radioButton = null;
				final EditText reasonEditText = new EditText(qa);
				reasonEditText.setVisibility(View.GONE);
				reasonEditText.setTag(qusId + "_reason");
				int len = qusSets.length;
				for (int i = 0; i < qusSets.length; i++) {
					radioButton = new RadioButton(qa);
					if (i != qusSets.length - 1) {
						radioButton.setTag(i + 1);
					} else {
						radioButton.setTag(0);
					}
					radioButton.setTextSize(itemSize);

					radioButton.setText(qusSets[i]);
					radioButton.setTextColor(Color.parseColor("#545124"));
					radioButton.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							RadioButton buttonView = (RadioButton) v;
							LinearLayout radioArea = (LinearLayout) buttonView
									.getParent().getParent();
							LinearLayout templinLayout;
							RadioButton radio = null;
							View view = null;
							for (int z = 0; z < radioArea.getChildCount(); z++) {
								view = radioArea.getChildAt(z);
								if (view instanceof LinearLayout) {
									templinLayout = (LinearLayout) radioArea
											.getChildAt(z);
									for (int y = 0; y < templinLayout
											.getChildCount(); y++) {
										radio = (RadioButton) templinLayout
												.getChildAt(y);
										if (radio != buttonView) {
											radio.setChecked(false);
										}
										Log.i("info", radio.getTag().toString());
									}
								}
							}
							if ("0".equalsIgnoreCase(buttonView.getTag()
									.toString())) {
								if (buttonView.isChecked()) {
									if (reasonEditText != null) {
										reasonEditText
												.setVisibility(View.VISIBLE);
									}
								} else {
									if (reasonEditText != null) {
										reasonEditText.setVisibility(View.GONE);
									}
								}
							} else {
								if (reasonEditText != null) {
									reasonEditText.setVisibility(View.GONE);
								}
							}
							Log.i("current", buttonView.getTag().toString()
									+ "  -----------");
						}
					});
					if (len > 6) {
						if (index % 6 == 0 && index != 1) {
							templLayout = new LinearLayout(qa);
						}
						if (index <= 6) {
							linearLayout2.addView(radioButton);
						} else {
							templLayout.addView(radioButton);
						}
						if (index == 6) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}

						if (index % 7 == 0) {
							container.addView(templLayout, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					} else {
						linearLayout2.addView(radioButton);
						if (index == len - 1) {
							container.addView(linearLayout2, new LayoutParams(
									LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
						}
					}
					index++;

				}
				container.addView(reasonEditText, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				linearLayout.addView(container, new LayoutParams(
						LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
				// linearLayout.addView(reasonEditText, new LayoutParams(
				// LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			}
			break;
		}
		return linearLayout;
	}

	/**
	 * 綁定圖片
	 * 
	 * @param file
	 * @param type
	 *            1：頭部 2:尾部
	 * @return
	 */
	public void createBindImage(File file, int type) {
		Qapaper activity = (Qapaper) qa;
		if (file.exists()) {
			switch (type) {
			case 1:
				ImageView head = (ImageView) activity
						.findViewById(d.qapaper.R.id.head);
				head.setImageBitmap(ImageUtil.getRoundedCornerBitmap(file
						.getPath().toString(), 0, 2));
				break;
			// case 2:
			// ImageView foot = (ImageView) activity
			// .findViewById(d.qapaper.R.id.foot);
			// foot.setImageBitmap(ImageUtil.getRoundedCornerBitmap(file
			// .getPath().toString(), 6, 2));
			// break;
			}
		}
	}

	/**
	 * 生成頭部服務項目選項
	 * 
	 * @return
	 */
	public LinearLayout createProjectInfo(List<Project> list,
			HashMap<String, String> config) {
		itemSize = 20;
		if (null != config.get("project")) {
			itemSize = Integer.parseInt(config.get("project"));
		}
		LinearLayout linearLayout2 = new LinearLayout(qa);
		LinearLayout container = new LinearLayout(qa);
		linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
		container.setOrientation(LinearLayout.VERTICAL);
		container.setTag("peoject_area");
		container.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		TextView titleView = new TextView(qa);
		titleView.setTextColor(Color.parseColor("#545124"));
		titleView.setTextSize(itemSize);
		titleView.setText("服務項目:");
		container.addView(titleView);
		CheckBox checkBox = null;
		final EditText otherEditText = new EditText(qa);
		otherEditText.setVisibility(View.GONE);
		otherEditText.setTag("project_other");
		// 循环linelayout
		LinearLayout templLayout = null;
		int i = 1;
		int len = list.size();
		for (Project project : list) {
			checkBox = new CheckBox(qa);
			checkBox.setTextColor(Color.parseColor("#545124"));
			checkBox.setTag(project.getId());
			checkBox.setTextSize(itemSize);
			checkBox.setText(project.getName());

			if ("1".equalsIgnoreCase(checkBox.getTag().toString())) {
				checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						// TODO Auto-generated method stub
						if (((CheckBox) buttonView).isChecked()) {
							if (otherEditText != null) {
								otherEditText.setVisibility(View.VISIBLE);
							}
						} else {
							if (otherEditText != null) {
								otherEditText.setVisibility(View.GONE);
							}
						}
					}
				});
			}
			if (len > 4) {
				if (i % 4 == 0 && i != 1) {
					templLayout = new LinearLayout(qa);
				}
				if (i <= 4) {
					linearLayout2.addView(checkBox);
				} else {
					templLayout.addView(checkBox);
				}
				if (i == 4) {
					container.addView(linearLayout2,
							new LayoutParams(LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
				}

				if (i % 5 == 0) {
					container.addView(templLayout,
							new LayoutParams(LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
				}
			} else {
				linearLayout2.addView(checkBox);
				if (i == len - 1) {
					container.addView(linearLayout2,
							new LayoutParams(LayoutParams.FILL_PARENT,
									LayoutParams.WRAP_CONTENT));
				}
			}

			i++;
		}

		container.addView(otherEditText, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		return container;
	}

	public LinearLayout createHeadUnit(List<Unit> list,
			HashMap<String, String> config) {
		itemSize = 20;
		if (null != config.get("unit")) {
			itemSize = Integer.parseInt(config.get("unit"));
		}
		LinearLayout linearLayout_title = new LinearLayout(qa);
		linearLayout_title.setOrientation(LinearLayout.VERTICAL);
		linearLayout_title.setTag("unit_area");
		linearLayout_title.setLayoutParams(new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		TextView titleView = new TextView(qa);
		titleView.setTextColor(Color.parseColor("#545124"));
		titleView.setTextSize(itemSize);
		titleView.setText("服務地點:");
		linearLayout_title.addView(titleView);
		final EditText otherEditText = new EditText(qa);
		otherEditText.setVisibility(View.GONE);
		otherEditText.setTag("project_other");
		Spinner spinner = new Spinner(qa);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(qa,
				android.R.layout.simple_spinner_item);
		for (Unit unit : list) {
			adapter.add(unit.getName());
		}
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		// spinner.set
		spinner.setSelection(1);
		// 添加監聽器
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				Spinner spinner = (Spinner) arg0;
				TextView text=(TextView)arg1;
				text.setTextSize(itemSize);
				if (0 == spinner.getSelectedItemPosition()) {
					otherEditText.setVisibility(View.VISIBLE);

				} else {
					otherEditText.setVisibility(View.GONE);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});
		linearLayout_title.addView(spinner, new LayoutParams(
				LayoutParams.FILL_PARENT, 20));
		linearLayout_title.addView(otherEditText, new LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
		return linearLayout_title;
	}

}
